package model;

import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;

import model.ProduccionBase.TipoBase;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

public class HojaDespacho {


	public HojaDespacho() {

	}

	public boolean crearHoja(File file,List<String> sabores, List<String> formas, ArrayList<ArrayList<JTable>> allTables,DateTime dia, String puntoDeVenta) {
		
		ArrayList<JTable> tables = allTables.get(0);
		
		Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
		PdfWriter writer = null;
		try {
			//String dir = System.getProperty("user.dir");
			writer = PdfWriter.getInstance(document,
					new FileOutputStream(file.getCanonicalPath() + ".pdf"));
		} catch (FileNotFoundException e) {
			JOptionPane
			.showMessageDialog(
					new JFrame(),
					"Archivo no encontrado o en uso",
					"Mensaje de Error",
					JOptionPane.ERROR_MESSAGE);
			return false;
		} catch (DocumentException e) {
			JOptionPane
			.showMessageDialog(
					new JFrame(),
					"Excepcion de Documento",
					"Mensaje de Error",
					JOptionPane.ERROR_MESSAGE);
			return false;
		} catch (IOException e) {
			JOptionPane
			.showMessageDialog(
					new JFrame(),
					"Error de IO",
					"Mensaje de Error",
					JOptionPane.ERROR_MESSAGE);
			return false;
		}

		LinkedHashMap<String,Integer> pesoFormaMap =  new LinkedHashMap<String,Integer>();


		for(Pudin.PesoForma pesoForma: Pudin.PesoForma.values()) {
			pesoFormaMap.put(pesoForma.nombre(),0);
		}

		document.open();
		PdfContentByte cb = writer.getDirectContent();

		ColumnText ct = new ColumnText(cb);
		
		// Title
		
		Phrase myText = new Phrase("PUNTO DE VENTA: " + puntoDeVenta.toUpperCase() , 
				FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD));
		ct.setSimpleColumn(myText, 325, 575, 555, 50, 10, Element.ALIGN_LEFT);

		try {
			ct.go();
		} catch (DocumentException e1) {
			e1.printStackTrace();
		}

		// Date

		DateTimeFormatter fmt = DateTimeFormat.forPattern("dd/MM/yyyy");
		String str = fmt.print(dia);

		myText = new Phrase("FECHA: " + str, FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD));

		ct.setSimpleColumn(myText, 800, 575, 555, 50, 10, Element.ALIGN_RIGHT);

		try {
			ct.go();
		} catch (DocumentException e1) {
			e1.printStackTrace();
		}
	
		// Columna Tamano
		
		ct = new ColumnText(cb);
		
		myText = new Phrase("Postres\n\n\n\n\n", FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD));		
		
		for(int i=0; i<sabores.size(); i++) {
			myText.add(sabores.get(i)+"\n");
		}	
			
		ct.setSimpleColumn(myText, 35, 400, 355, 50, 10, Element.ALIGN_LEFT);

		try {
			ct.go();
		} catch (DocumentException e1) {
			e1.printStackTrace();
		}
		
		for(int i=0; i<formas.size(); i++) {
			
			ct = new ColumnText(cb);
			
			int countLines = countLines(formas.get(i));
			int firstNum = 65,secondNum = 70;
			if(countLines == 1) {
				formas.set(i, ((String)formas.get(i)).replace(" ", "\n"));
				countLines = countLines(formas.get(i));
				
			}
			else if(countLines==2) {
				String[] lines = formas.get(i).split("\n");
				if(lines[0].startsWith(lines[1])) {
					formas.set(i, lines[0]);
				}
				else if(lines[0].endsWith("LB")) {
					formas.set(i, lines[0].substring(0, lines[0].length()-3) + "\n" + lines[1]);
				}
				else if(lines[0].endsWith("LB ")) {
					formas.set(i, lines[0].substring(0, lines[0].length()-4) + "\n" + lines[1]);
				}
				
				if(lines[0].startsWith("MINI")) {
					formas.set(i, "MINI");
				}
				if(lines[0].startsWith("PORCION")) {
					formas.set(i, "PORC");
				}
				if(lines[0].startsWith("CUPCAKE")) {
					formas.set(i, "CUP");
				}
				
				countLines = countLines(formas.get(i));
				firstNum = 35;
			}
			
			myText = new Phrase(formas.get(i) + repeatString("\n", 5-countLines), FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD));
			
			for(int j=0; j<sabores.size(); j++) {
				myText.add("---"+"\n");
			}
			
			ct.setSimpleColumn(myText, 160+(i*firstNum), 400, 525+(i*secondNum), 50, 10, Element.ALIGN_LEFT);
			try {
				ct.go();
			} catch (DocumentException e1) {
				e1.printStackTrace();
			}

			
		}



		document.close();

		return true;

	}
	
	private static int countLines(String str){
		   String[] lines = str.split("\r\n|\r|\n");
		   return  lines.length;
		}
	
	private static String repeatString(String str, int times){
		String result = str;
		for(int i=0;i<times;i++) {
			   result += str;
		}
		   return  result;
	}



}
